<template>
  <div class="list" ref="wrapper">
    <div ref="content">
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{ this.city }}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper" v-for="item of hotCities" :key="item.id">
            <div class="button" @click="handleCityClick(item.name)">{{item.name}}</div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(city,key) of cities" :key="key" :ref="key">
        <div class="title border-topbottom">{{key}}</div>
        <div class="item-list">
          <div
            class="item border-bottom"
            @click="handleCityClick(item.name)"
            v-for="item of city"
            :key="item.id"
          >{{ item.name }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Bscroll from "better-scroll";
import { mapState, mapMutations } from "vuex";
export default {
  name: "CityList",
  props: ["cities", "hotCities", "cityIndex"],
  data() {
    return {};
  },
  methods: {
    handleCityClick(city) {
      this.changeCity(city);
      this.$router.push("/");
    },
    ...mapMutations(["changeCity"])
  },
  watch: {
    cityIndex() {
      const element = this.$refs[this.cityIndex][0];
      this.scroll.scrollToElement(element);
    }
  },
  computed: {
    ...mapState(["city"])
  },
  mounted() {
    this.scroll = new Bscroll(this.$refs.wrapper, {
      click: true
    });

    //延时启动，解决better-scroll初始化无法进行滚动
    setTimeout(() => {
      if (!this.scroll) {
        this.scroll = new Bscroll(this.$refs["search-content"], {
          click: true
        });
      } else {
        this.scroll.refresh();
      }
    }, 500);
  },
  created() {}
};
</script>

<style lang="stylus" scoped>
.border-topbottom {
  &:before {
    border-color: #ccc;
  }

  &:after {
    border-color: #ccc;
  }
}

.border-bottom {
  &:before {
    border-color: #ccc;
  }
}

.list {
  position: absolute;
  top: 1.78rem;
  left: 0;
  bottom: 0;
  right: 0;
  overflow: hidden;

  .title {
    line-height: 0.44rem;
    background-color: #eee;
    padding-left: 0.2rem;
    color: #666;
    font-size: 0.26rem;
  }

  .button-list {
    padding: 0.1rem 0.6rem 0.1rem 0.1rem;
    overflow: hidden;

    .button-wrapper {
      float: left;
      width: 33.3%;

      .button {
        margin: 0.1rem;
        padding: 0.1rem;
        text-align: center;
        border: 0.02rem solid #ccc;
        border-radius: 0.06rem;
        z-index: 1;
      }
    }
  }

  .item-list {
    .item {
      line-height: 0.56rem;
      padding: 0.2rem;
    }
  }
}
</style>
